

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="Description" content="scikit-learn: machine learning in Python">

  
  <title>An introduction to machine learning with scikit-learn &mdash; scikit-learn 0.22 documentation</title>
  
  <link rel="canonical" href="http://scikit-learn.org/stable/tutorial/basic/tutorial.html" />

  
  <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  

  <link rel="stylesheet" href="../../_static/css/vendor/bootstrap.min.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
<script src="../../_static/jquery.js"></script> 
</head>
<body>
<nav id="navbar" class="sk-docs-navbar navbar navbar-expand-md navbar-light bg-light py-0">
  <div class="container-fluid sk-docs-container px-0">
      <a class="navbar-brand py-0" href="../../index.html">
        <img
          class="sk-brand-img"
          src="../../_static/scikit-learn-logo-small.png"
          alt="logo"/>
      </a>
    <button
      id="sk-navbar-toggler"
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="sk-navbar-collapse collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../../install.html">Install</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../../user_guide.html">User Guide</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../../modules/classes.html">API</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../../auto_examples/index.html">Examples</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../getting_started.html">Getting Started</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../index.html">Tutorial</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../glossary.html">Glossary</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../developers/index.html">Development</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../faq.html">FAQ</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../related_projects.html">Related packages</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../roadmap.html">Roadmap</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../about.html">About us</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
        </li>
        <li class="nav-item dropdown nav-more-item-dropdown">
          <a class="sk-nav-link nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">More</a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              <a class="sk-nav-dropdown-item dropdown-item" href="../../getting_started.html">Getting Started</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../index.html">Tutorial</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../../glossary.html">Glossary</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../../developers/index.html">Development</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../../faq.html">FAQ</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../../related_projects.html">Related packages</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../../roadmap.html">Roadmap</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../../about.html">About us</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
          </div>
        </li>
      </ul>
      <div id="searchbox" role="search">
          <div class="searchformwrapper">
          <form class="search" action="../../search.html" method="get">
            <input class="sk-search-text-input" type="text" name="q" aria-labelledby="searchlabel" />
            <input class="sk-search-text-btn" type="submit" value="Go" />
          </form>
          </div>
      </div>
    </div>
  </div>
</nav>
<div class="d-flex" id="sk-doc-wrapper">
    <input type="checkbox" name="sk-toggle-checkbox" id="sk-toggle-checkbox">
    <label id="sk-sidemenu-toggle" class="sk-btn-toggle-toc btn sk-btn-primary" for="sk-toggle-checkbox">Toggle Menu</label>
    <div id="sk-sidebar-wrapper" class="border-right">
      <div class="sk-sidebar-toc-wrapper">
        <div class="sk-sidebar-toc-logo">
          <a href="../../index.html">
            <img
              class="sk-brand-img"
              src="../../_static/scikit-learn-logo-small.png"
              alt="logo"/>
          </a>
        </div>
        <div class="btn-group w-100 mb-2" role="group" aria-label="rellinks">
            <a href="../index.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="scikit-learn Tutorials">Prev</a><a href="../index.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="scikit-learn Tutorials">Up</a>
            <a href="../statistical_inference/index.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="A tutorial on statistical-learning for scientific data processing">Next</a>
        </div>
        <div class="alert alert-danger p-1 mb-2" role="alert">
          <p class="text-center mb-0">
          <strong>scikit-learn 0.22</strong><br/>
          <a href="http://scikit-learn.org/dev/versions.html">Other versions</a>
          </p>
        </div>
        <div class="alert alert-warning p-1 mb-2" role="alert">
          <p class="text-center mb-0">
            Please <a class="font-weight-bold" href="../../about.html#citing-scikit-learn"><string>cite us</string></a> if you use the software.
          </p>
        </div>
          <div class="sk-sidebar-toc">
            <ul>
<li><a class="reference internal" href="#">An introduction to machine learning with scikit-learn</a><ul>
<li><a class="reference internal" href="#machine-learning-the-problem-setting">Machine learning: the problem setting</a></li>
<li><a class="reference internal" href="#loading-an-example-dataset">Loading an example dataset</a></li>
<li><a class="reference internal" href="#learning-and-predicting">Learning and predicting</a></li>
<li><a class="reference internal" href="#model-persistence">Model persistence</a></li>
<li><a class="reference internal" href="#conventions">Conventions</a><ul>
<li><a class="reference internal" href="#type-casting">Type casting</a></li>
<li><a class="reference internal" href="#refitting-and-updating-parameters">Refitting and updating parameters</a></li>
<li><a class="reference internal" href="#multiclass-vs-multilabel-fitting">Multiclass vs. multilabel fitting</a></li>
</ul>
</li>
</ul>
</li>
</ul>

          </div>
      </div>
    </div>
    <div id="sk-page-content-wrapper">
      <div class="sk-page-content container-fluid body px-md-3" role="main">
        
  <div class="section" id="an-introduction-to-machine-learning-with-scikit-learn">
<span id="introduction"></span><h1>An introduction to machine learning with scikit-learn<a class="headerlink" href="#an-introduction-to-machine-learning-with-scikit-learn" title="Permalink to this headline">¶</a></h1>
<div class="topic">
<p class="topic-title">Section contents</p>
<p>In this section, we introduce the <a class="reference external" href="https://en.wikipedia.org/wiki/Machine_learning">machine learning</a>
vocabulary that we use throughout scikit-learn and give a
simple learning example.</p>
</div>
<div class="section" id="machine-learning-the-problem-setting">
<h2>Machine learning: the problem setting<a class="headerlink" href="#machine-learning-the-problem-setting" title="Permalink to this headline">¶</a></h2>
<p>In general, a learning problem considers a set of n
<a class="reference external" href="https://en.wikipedia.org/wiki/Sample_(statistics)">samples</a> of
data and then tries to predict properties of unknown data. If each sample is
more than a single number and, for instance, a multi-dimensional entry
(aka <a class="reference external" href="https://en.wikipedia.org/wiki/Multivariate_random_variable">multivariate</a>
data), it is said to have several attributes or <strong>features</strong>.</p>
<p>Learning problems fall into a few categories:</p>
<blockquote>
<div><ul>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Supervised_learning">supervised learning</a>,
in which the data comes with additional attributes that we want to predict
(<a class="reference internal" href="../../supervised_learning.html#supervised-learning"><span class="std std-ref">Click here</span></a>
to go to the scikit-learn supervised learning page).This problem
can be either:</p>
<blockquote>
<div><ul class="simple">
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Classification_in_machine_learning">classification</a>:
samples belong to two or more classes and we
want to learn from already labeled data how to predict the class
of unlabeled data. An example of a classification problem would
be handwritten digit recognition, in which the aim is
to assign each input vector to one of a finite number of discrete
categories.  Another way to think of classification is as a discrete
(as opposed to continuous) form of supervised learning where one has a
limited number of categories and for each of the n samples provided,
one is to try to label them with the correct category or class.</p></li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Regression_analysis">regression</a>:
if the desired output consists of one or more
continuous variables, then the task is called <em>regression</em>. An
example of a regression problem would be the prediction of the
length of a salmon as a function of its age and weight.</p></li>
</ul>
</div></blockquote>
</li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Unsupervised_learning">unsupervised learning</a>,
in which the training data consists of a set of input vectors x
without any corresponding target values. The goal in such problems
may be to discover groups of similar examples within the data, where
it is called <a class="reference external" href="https://en.wikipedia.org/wiki/Cluster_analysis">clustering</a>,
or to determine the distribution of data within the input space, known as
<a class="reference external" href="https://en.wikipedia.org/wiki/Density_estimation">density estimation</a>, or
to project the data from a high-dimensional space down to two or three
dimensions for the purpose of <em>visualization</em>
(<a class="reference internal" href="../../unsupervised_learning.html#unsupervised-learning"><span class="std std-ref">Click here</span></a>
to go to the Scikit-Learn unsupervised learning page).</p></li>
</ul>
</div></blockquote>
<div class="topic">
<p class="topic-title">Training set and testing set</p>
<p>Machine learning is about learning some properties of a data set
and then testing those properties against another data set. A common
practice in machine learning is to evaluate an algorithm by splitting a data
set into two. We call one of those sets the <strong>training set</strong>, on which we
learn some properties; we call the other set the <strong>testing set</strong>, on which
we test the learned properties.</p>
</div>
</div>
<div class="section" id="loading-an-example-dataset">
<span id="loading-example-dataset"></span><h2>Loading an example dataset<a class="headerlink" href="#loading-an-example-dataset" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">scikit-learn</span></code> comes with a few standard datasets, for instance the
<a class="reference external" href="https://en.wikipedia.org/wiki/Iris_flower_data_set">iris</a> and <a class="reference external" href="https://archive.ics.uci.edu/ml/datasets/Pen-Based+Recognition+of+Handwritten+Digits">digits</a>
datasets for classification and the <a class="reference external" href="https://archive.ics.uci.edu/ml/machine-learning-databases/housing/">boston house prices dataset</a> for regression.</p>
<p>In the following, we start a Python interpreter from our shell and then
load the <code class="docutils literal notranslate"><span class="pre">iris</span></code> and <code class="docutils literal notranslate"><span class="pre">digits</span></code> datasets.  Our notational convention is that
<code class="docutils literal notranslate"><span class="pre">$</span></code> denotes the shell prompt while <code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code> denotes the Python
interpreter prompt:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ python
&gt;&gt;&gt; from sklearn import datasets
&gt;&gt;&gt; iris = datasets.load_iris()
&gt;&gt;&gt; digits = datasets.load_digits()
</pre></div>
</div>
<p>A dataset is a dictionary-like object that holds all the data and some
metadata about the data. This data is stored in the <code class="docutils literal notranslate"><span class="pre">.data</span></code> member,
which is a <code class="docutils literal notranslate"><span class="pre">n_samples,</span> <span class="pre">n_features</span></code> array. In the case of supervised
problem, one or more response variables are stored in the <code class="docutils literal notranslate"><span class="pre">.target</span></code> member. More
details on the different datasets can be found in the <a class="reference internal" href="../../datasets/index.html#datasets"><span class="std std-ref">dedicated
section</span></a>.</p>
<p>For instance, in the case of the digits dataset, <code class="docutils literal notranslate"><span class="pre">digits.data</span></code> gives
access to the features that can be used to classify the digits samples:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">digits</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
<span class="go">[[ 0.   0.   5. ...   0.   0.   0.]</span>
<span class="go"> [ 0.   0.   0. ...  10.   0.   0.]</span>
<span class="go"> [ 0.   0.   0. ...  16.   9.   0.]</span>
<span class="go"> ...</span>
<span class="go"> [ 0.   0.   1. ...   6.   0.   0.]</span>
<span class="go"> [ 0.   0.   2. ...  12.   0.   0.]</span>
<span class="go"> [ 0.   0.  10. ...  12.   1.   0.]]</span>
</pre></div>
</div>
<p>and <code class="docutils literal notranslate"><span class="pre">digits.target</span></code> gives the ground truth for the digit dataset, that
is the number corresponding to each digit image that we are trying to
learn:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="o">.</span><span class="n">target</span>
<span class="go">array([0, 1, 2, ..., 8, 9, 8])</span>
</pre></div>
</div>
<div class="topic">
<p class="topic-title">Shape of the data arrays</p>
<p>The data is always a 2D array, shape <code class="docutils literal notranslate"><span class="pre">(n_samples,</span> <span class="pre">n_features)</span></code>, although
the original data may have had a different shape. In the case of the
digits, each original sample is an image of shape <code class="docutils literal notranslate"><span class="pre">(8,</span> <span class="pre">8)</span></code> and can be
accessed using:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="o">.</span><span class="n">images</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">array([[  0.,   0.,   5.,  13.,   9.,   1.,   0.,   0.],</span>
<span class="go">       [  0.,   0.,  13.,  15.,  10.,  15.,   5.,   0.],</span>
<span class="go">       [  0.,   3.,  15.,   2.,   0.,  11.,   8.,   0.],</span>
<span class="go">       [  0.,   4.,  12.,   0.,   0.,   8.,   8.,   0.],</span>
<span class="go">       [  0.,   5.,   8.,   0.,   0.,   9.,   8.,   0.],</span>
<span class="go">       [  0.,   4.,  11.,   0.,   1.,  12.,   7.,   0.],</span>
<span class="go">       [  0.,   2.,  14.,   5.,  10.,  12.,   0.,   0.],</span>
<span class="go">       [  0.,   0.,   6.,  13.,  10.,   0.,   0.,   0.]])</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../../auto_examples/classification/plot_digits_classification.html#sphx-glr-auto-examples-classification-plot-digits-classification-py"><span class="std std-ref">simple example on this dataset</span></a> illustrates how starting
from the original problem one can shape the data for consumption in
scikit-learn.</p>
</div>
<div class="topic">
<p class="topic-title">Loading from external datasets</p>
<p>To load from an external dataset, please refer to <a class="reference internal" href="../../datasets/index.html#external-datasets"><span class="std std-ref">loading external datasets</span></a>.</p>
</div>
</div>
<div class="section" id="learning-and-predicting">
<h2>Learning and predicting<a class="headerlink" href="#learning-and-predicting" title="Permalink to this headline">¶</a></h2>
<p>In the case of the digits dataset, the task is to predict, given an image,
which digit it represents. We are given samples of each of the 10
possible classes (the digits zero through nine) on which we <em>fit</em> an
<a class="reference external" href="https://en.wikipedia.org/wiki/Estimator">estimator</a> to be able to <em>predict</em>
the classes to which unseen samples belong.</p>
<p>In scikit-learn, an estimator for classification is a Python object that
implements the methods <code class="docutils literal notranslate"><span class="pre">fit(X,</span> <span class="pre">y)</span></code> and <code class="docutils literal notranslate"><span class="pre">predict(T)</span></code>.</p>
<p>An example of an estimator is the class <code class="docutils literal notranslate"><span class="pre">sklearn.svm.SVC</span></code>, which
implements <a class="reference external" href="https://en.wikipedia.org/wiki/Support_vector_machine">support vector classification</a>. The
estimator’s constructor takes as arguments the model’s parameters.</p>
<p>For now, we will consider the estimator as a black box:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">svm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">svm</span><span class="o">.</span><span class="n">SVC</span><span class="p">(</span><span class="n">gamma</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">C</span><span class="o">=</span><span class="mf">100.</span><span class="p">)</span>
</pre></div>
</div>
<div class="topic">
<p class="topic-title">Choosing the parameters of the model</p>
<p>In this example, we set the value of <code class="docutils literal notranslate"><span class="pre">gamma</span></code> manually.
To find good values for these parameters, we can use tools
such as <a class="reference internal" href="../../modules/grid_search.html#grid-search"><span class="std std-ref">grid search</span></a> and <a class="reference internal" href="../../modules/cross_validation.html#cross-validation"><span class="std std-ref">cross validation</span></a>.</p>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">clf</span></code> (for classifier) estimator instance is first
fitted to the model; that is, it must <em>learn</em> from the model. This is
done by passing our training set to the <code class="docutils literal notranslate"><span class="pre">fit</span></code> method. For the training
set, we’ll use all the images from our dataset, except for the last
image, which we’ll reserve for our predicting. We select the training set with
the <code class="docutils literal notranslate"><span class="pre">[:-1]</span></code> Python syntax, which produces a new array that contains all but
the last item from <code class="docutils literal notranslate"><span class="pre">digits.data</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">digits</span><span class="o">.</span><span class="n">data</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">digits</span><span class="o">.</span><span class="n">target</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="go">SVC(C=100.0, gamma=0.001)</span>
</pre></div>
</div>
<p>Now you can <em>predict</em> new values. In this case, you’ll predict using the last
image from <code class="docutils literal notranslate"><span class="pre">digits.data</span></code>. By predicting, you’ll determine the image from the
training set that best matches the last image.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">digits</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">:])</span>
<span class="go">array([8])</span>
</pre></div>
</div>
<p>The corresponding image is:</p>
<a class="reference external image-reference" href="../../auto_examples/datasets/plot_digits_last_image.html"><img alt="auto_examples/datasets/images/sphx_glr_plot_digits_last_image_001.png" class="align-center" src="auto_examples/datasets/images/sphx_glr_plot_digits_last_image_001.png" /></a>
<p>As you can see, it is a challenging task: after all, the images are of poor
resolution. Do you agree with the classifier?</p>
<p>A complete example of this classification problem is available as an
example that you can run and study:
<a class="reference internal" href="../../auto_examples/classification/plot_digits_classification.html#sphx-glr-auto-examples-classification-plot-digits-classification-py"><span class="std std-ref">Recognizing hand-written digits</span></a>.</p>
</div>
<div class="section" id="model-persistence">
<h2>Model persistence<a class="headerlink" href="#model-persistence" title="Permalink to this headline">¶</a></h2>
<p>It is possible to save a model in scikit-learn by using Python’s built-in
persistence model, <a class="reference external" href="https://docs.python.org/2/library/pickle.html">pickle</a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">svm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">datasets</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">svm</span><span class="o">.</span><span class="n">SVC</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">load_iris</span><span class="p">(</span><span class="n">return_X_y</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">SVC()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pickle</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">clf</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf2</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf2</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">1</span><span class="p">])</span>
<span class="go">array([0])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">0</span>
</pre></div>
</div>
<p>In the specific case of scikit-learn, it may be more interesting to use
joblib’s replacement for pickle (<code class="docutils literal notranslate"><span class="pre">joblib.dump</span></code> &amp; <code class="docutils literal notranslate"><span class="pre">joblib.load</span></code>),
which is more efficient on big data but it can only pickle to the disk
and not to a string:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">joblib</span> <span class="kn">import</span> <span class="n">dump</span><span class="p">,</span> <span class="n">load</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dump</span><span class="p">(</span><span class="n">clf</span><span class="p">,</span> <span class="s1">&#39;filename.joblib&#39;</span><span class="p">)</span> 
</pre></div>
</div>
<p>Later, you can reload the pickled model (possibly in another Python process)
with:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="s1">&#39;filename.joblib&#39;</span><span class="p">)</span> 
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">joblib.dump</span></code> and <code class="docutils literal notranslate"><span class="pre">joblib.load</span></code> functions also accept file-like object
instead of filenames. More information on data persistence with Joblib is
available <a class="reference external" href="https://joblib.readthedocs.io/en/latest/persistence.html">here</a>.</p>
</div>
<p>Note that pickle has some security and maintainability issues. Please refer to
section <a class="reference internal" href="../../modules/model_persistence.html#model-persistence"><span class="std std-ref">Model persistence</span></a> for more detailed information about model
persistence with scikit-learn.</p>
</div>
<div class="section" id="conventions">
<h2>Conventions<a class="headerlink" href="#conventions" title="Permalink to this headline">¶</a></h2>
<p>scikit-learn estimators follow certain rules to make their behavior more
predictive.  These are described in more detail in the <a class="reference internal" href="../../glossary.html#glossary"><span class="std std-ref">Glossary of Common Terms and API Elements</span></a>.</p>
<div class="section" id="type-casting">
<h3>Type casting<a class="headerlink" href="#type-casting" title="Permalink to this headline">¶</a></h3>
<p>Unless otherwise specified, input will be cast to <code class="docutils literal notranslate"><span class="pre">float64</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">random_projection</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">rng</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">RandomState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">2000</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;float32&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span><span class="o">.</span><span class="n">dtype</span>
<span class="go">dtype(&#39;float32&#39;)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">transformer</span> <span class="o">=</span> <span class="n">random_projection</span><span class="o">.</span><span class="n">GaussianRandomProjection</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X_new</span> <span class="o">=</span> <span class="n">transformer</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X_new</span><span class="o">.</span><span class="n">dtype</span>
<span class="go">dtype(&#39;float64&#39;)</span>
</pre></div>
</div>
<p>In this example, <code class="docutils literal notranslate"><span class="pre">X</span></code> is <code class="docutils literal notranslate"><span class="pre">float32</span></code>, which is cast to <code class="docutils literal notranslate"><span class="pre">float64</span></code> by
<code class="docutils literal notranslate"><span class="pre">fit_transform(X)</span></code>.</p>
<p>Regression targets are cast to <code class="docutils literal notranslate"><span class="pre">float64</span></code> and classification targets are
maintained:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">datasets</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.svm</span> <span class="kn">import</span> <span class="n">SVC</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">iris</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">load_iris</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">SVC</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">iris</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>
<span class="go">SVC()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">[:</span><span class="mi">3</span><span class="p">]))</span>
<span class="go">[0, 0, 0]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">iris</span><span class="o">.</span><span class="n">target_names</span><span class="p">[</span><span class="n">iris</span><span class="o">.</span><span class="n">target</span><span class="p">])</span>
<span class="go">SVC()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">[:</span><span class="mi">3</span><span class="p">]))</span>
<span class="go">[&#39;setosa&#39;, &#39;setosa&#39;, &#39;setosa&#39;]</span>
</pre></div>
</div>
<p>Here, the first <code class="docutils literal notranslate"><span class="pre">predict()</span></code> returns an integer array, since <code class="docutils literal notranslate"><span class="pre">iris.target</span></code>
(an integer array) was used in <code class="docutils literal notranslate"><span class="pre">fit</span></code>. The second <code class="docutils literal notranslate"><span class="pre">predict()</span></code> returns a string
array, since <code class="docutils literal notranslate"><span class="pre">iris.target_names</span></code> was for fitting.</p>
</div>
<div class="section" id="refitting-and-updating-parameters">
<h3>Refitting and updating parameters<a class="headerlink" href="#refitting-and-updating-parameters" title="Permalink to this headline">¶</a></h3>
<p>Hyper-parameters of an estimator can be updated after it has been constructed
via the <a class="reference internal" href="../../glossary.html#term-set-params"><span class="xref std std-term">set_params()</span></a> method. Calling <code class="docutils literal notranslate"><span class="pre">fit()</span></code> more than
once will overwrite what was learned by any previous <code class="docutils literal notranslate"><span class="pre">fit()</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">load_iris</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.svm</span> <span class="kn">import</span> <span class="n">SVC</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">load_iris</span><span class="p">(</span><span class="n">return_X_y</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">SVC</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">set_params</span><span class="p">(</span><span class="n">kernel</span><span class="o">=</span><span class="s1">&#39;linear&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">SVC(kernel=&#39;linear&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X</span><span class="p">[:</span><span class="mi">5</span><span class="p">])</span>
<span class="go">array([0, 0, 0, 0, 0])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">set_params</span><span class="p">(</span><span class="n">kernel</span><span class="o">=</span><span class="s1">&#39;rbf&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">SVC()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X</span><span class="p">[:</span><span class="mi">5</span><span class="p">])</span>
<span class="go">array([0, 0, 0, 0, 0])</span>
</pre></div>
</div>
<p>Here, the default kernel <code class="docutils literal notranslate"><span class="pre">rbf</span></code> is first changed to <code class="docutils literal notranslate"><span class="pre">linear</span></code> via
<a class="reference internal" href="../../modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC.set_params" title="sklearn.svm.SVC.set_params"><code class="xref py py-func docutils literal notranslate"><span class="pre">SVC.set_params()</span></code></a> after the estimator has
been constructed, and changed back to <code class="docutils literal notranslate"><span class="pre">rbf</span></code> to refit the estimator and to
make a second prediction.</p>
</div>
<div class="section" id="multiclass-vs-multilabel-fitting">
<h3>Multiclass vs. multilabel fitting<a class="headerlink" href="#multiclass-vs-multilabel-fitting" title="Permalink to this headline">¶</a></h3>
<p>When using <a class="reference internal" href="../../modules/classes.html#module-sklearn.multiclass" title="sklearn.multiclass"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiclass</span> <span class="pre">classifiers</span></code></a>,
the learning and prediction task that is performed is dependent on the format of
the target data fit upon:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.svm</span> <span class="kn">import</span> <span class="n">SVC</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.multiclass</span> <span class="kn">import</span> <span class="n">OneVsRestClassifier</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">LabelBinarizer</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">classif</span> <span class="o">=</span> <span class="n">OneVsRestClassifier</span><span class="p">(</span><span class="n">estimator</span><span class="o">=</span><span class="n">SVC</span><span class="p">(</span><span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">classif</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="go">array([0, 0, 1, 1, 2])</span>
</pre></div>
</div>
<p>In the above case, the classifier is fit on a 1d array of multiclass labels and
the <code class="docutils literal notranslate"><span class="pre">predict()</span></code> method therefore provides corresponding multiclass predictions.
It is also possible to fit upon a 2d array of binary label indicators:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">LabelBinarizer</span><span class="p">()</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">classif</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="go">array([[1, 0, 0],</span>
<span class="go">       [1, 0, 0],</span>
<span class="go">       [0, 1, 0],</span>
<span class="go">       [0, 0, 0],</span>
<span class="go">       [0, 0, 0]])</span>
</pre></div>
</div>
<p>Here, the classifier is <code class="docutils literal notranslate"><span class="pre">fit()</span></code>  on a 2d binary label representation of <code class="docutils literal notranslate"><span class="pre">y</span></code>,
using the <a class="reference internal" href="../../modules/generated/sklearn.preprocessing.LabelBinarizer.html#sklearn.preprocessing.LabelBinarizer" title="sklearn.preprocessing.LabelBinarizer"><code class="xref py py-class docutils literal notranslate"><span class="pre">LabelBinarizer</span></code></a>.
In this case <code class="docutils literal notranslate"><span class="pre">predict()</span></code> returns a 2d array representing the corresponding
multilabel predictions.</p>
<p>Note that the fourth and fifth instances returned all zeroes, indicating that
they matched none of the three labels <code class="docutils literal notranslate"><span class="pre">fit</span></code> upon. With multilabel outputs, it
is similarly possible for an instance to be assigned multiple labels:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">MultiLabelBinarizer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">MultiLabelBinarizer</span><span class="p">()</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">classif</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="go">array([[1, 1, 0, 0, 0],</span>
<span class="go">       [1, 0, 1, 0, 0],</span>
<span class="go">       [0, 1, 0, 1, 0],</span>
<span class="go">       [1, 0, 1, 0, 0],</span>
<span class="go">       [1, 0, 1, 0, 0]])</span>
</pre></div>
</div>
<p>In this case, the classifier is fit upon instances each assigned multiple labels.
The <a class="reference internal" href="../../modules/generated/sklearn.preprocessing.MultiLabelBinarizer.html#sklearn.preprocessing.MultiLabelBinarizer" title="sklearn.preprocessing.MultiLabelBinarizer"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultiLabelBinarizer</span></code></a> is
used to binarize the 2d array of multilabels to <code class="docutils literal notranslate"><span class="pre">fit</span></code> upon. As a result,
<code class="docutils literal notranslate"><span class="pre">predict()</span></code> returns a 2d array with multiple predicted labels for each instance.</p>
</div>
</div>
</div>


      </div>
    <div class="container">
      <footer class="sk-content-footer">
            &copy; 2007 - 2019, scikit-learn developers (BSD License).
          <a href="../../_sources/tutorial/basic/tutorial.rst.txt" rel="nofollow">Show this page source</a>
      </footer>
    </div>
  </div>
</div>
<script src="../../_static/js/vendor/bootstrap.min.js"></script>

<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-22606712-2', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>


<script>
$(document).ready(function() {
    /* Add a [>>>] button on the top-right corner of code samples to hide
     * the >>> and ... prompts and the output and thus make the code
     * copyable. */
    var div = $('.highlight-python .highlight,' +
                '.highlight-python3 .highlight,' +
                '.highlight-pycon .highlight,' +
		'.highlight-default .highlight')
    var pre = div.find('pre');

    // get the styles from the current theme
    pre.parent().parent().css('position', 'relative');
    var hide_text = 'Hide prompts and outputs';
    var show_text = 'Show prompts and outputs';

    // create and add the button to all the code blocks that contain >>>
    div.each(function(index) {
        var jthis = $(this);
        if (jthis.find('.gp').length > 0) {
            var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
            jthis.prepend(button);
        }
        // tracebacks (.gt) contain bare text elements that need to be
        // wrapped in a span to work with .nextUntil() (see later)
        jthis.find('pre:has(.gt)').contents().filter(function() {
            return ((this.nodeType == 3) && (this.data.trim().length > 0));
        }).wrap('<span>');
    });

    // define the behavior of the button when it's clicked
    $('.copybutton').click(function(e){
        e.preventDefault();
        var button = $(this);
        if (button.data('hidden') === 'false') {
            // hide the code output
            button.parent().find('.go, .gp, .gt').hide();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
            button.css('text-decoration', 'line-through');
            button.attr('title', show_text);
            button.data('hidden', 'true');
        } else {
            // show the code output
            button.parent().find('.go, .gp, .gt').show();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
            button.css('text-decoration', 'none');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
        }
    });

	/*** Add permalink buttons next to glossary terms ***/
	$('dl.glossary > dt[id]').append(function() {
		return ('<a class="headerlink" href="#' +
			    this.getAttribute('id') +
			    '" title="Permalink to this term">¶</a>');
	});
  /*** Hide navbar when scrolling down ***/
  // Returns true when headerlink target matches hash in url
  (function() {
    hashTargetOnTop = function() {
        var hash = window.location.hash;
        if ( hash.length < 2 ) { return false; }

        var target = document.getElementById( hash.slice(1) );
        if ( target === null ) { return false; }

        var top = target.getBoundingClientRect().top;
        return (top < 2) && (top > -2);
    };

    // Hide navbar on load if hash target is on top
    var navBar = document.getElementById("navbar");
    var navBarToggler = document.getElementById("sk-navbar-toggler");
    var navBarHeightHidden = "-" + navBar.getBoundingClientRect().height + "px";
    var $window = $(window);

    hideNavBar = function() {
        navBar.style.top = navBarHeightHidden;
    };

    showNavBar = function() {
        navBar.style.top = "0";
    }

    if (hashTargetOnTop()) {
        hideNavBar()
    }

    var prevScrollpos = window.pageYOffset;
    hideOnScroll = function(lastScrollTop) {
        if (($window.width() < 768) && (navBarToggler.getAttribute("aria-expanded") === 'true')) {
            return;
        }
        if (lastScrollTop > 2 && (prevScrollpos <= lastScrollTop) || hashTargetOnTop()){
            hideNavBar()
        } else {
            showNavBar()
        }
        prevScrollpos = lastScrollTop;
    };

    /*** high preformance scroll event listener***/
    var raf = window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        window.oRequestAnimationFrame;
    var lastScrollTop = $window.scrollTop();

    if (raf) {
        loop();
    }

    function loop() {
        var scrollTop = $window.scrollTop();
        if (lastScrollTop === scrollTop) {
            raf(loop);
            return;
        } else {
            lastScrollTop = scrollTop;
            hideOnScroll(lastScrollTop);
            raf(loop);
        }
    }
  })();
});

</script>
    
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
    
</body>
</html>